﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Script;

public partial class Theme_DL_MyClassExams : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected string ClassName = string.Empty;
    protected string CourseNum = string.Empty;
    private string CourseID = string.Empty;
    PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    VOTOClassExam voclassexam = new VOTOClassExam();
    TOClassExam objclassexam = new TOClassExam();
    private VOTOClass voClass = new VOTOClass();
    TOClass objClass = new TOClass();
    PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    protected void Page_Load(object sender, EventArgs e)
    {
        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        int CID = 0;
        if (Request.QueryString["ClassID"] != null)
        {
            CID = int.Parse(Request.QueryString["ClassID"].ToString());
            ViewState["ClassID"] = CID.ToString();
            voClass = objClass.Load(CID);
            ClassName = voClass.Name;
            BinderPG(voClass);
        }
    }





    private void BinderPG(VOTOClass vo)
    {
        VOTOCourseList voCourselist = new VOTOCourseList();
        TOCourseList objCourselist = new TOCourseList();
        voCourselist = objCourselist.Load(vo.ClassCourseListID);
        string CourseIDsList = voCourselist.CourseID;
        string[] courseid = CourseIDsList.Split('#');
        CourseNum = courseid.Length.ToString();
        string courseidlist = string.Empty;
        for (int i = 0; i < courseid.Length; i++)
        {
            string[] par = courseid[i].Split(',');
            if (par.Length == 4)
            {
                if (courseidlist.Length > 0)
                    courseidlist += ",";
                courseidlist += par[0];
            }
        }
        if (courseidlist.Length == 0)
            courseidlist = "0";


        //      

        string search = "SELECT TO_Course.Course_Id ,TO_Course.CourseName,Paper.PaperID, Paper.PaperName, TO_ClassExam.ClassExam_ID,TO_ClassExam.ClassID, TO_ClassExam.Exam_ID, TO_ClassExam.Score,TO_ClassExam.ExamTime FROM Paper,TO_ClassExam,TO_Course where Paper.PaperID = TO_ClassExam.Exam_ID and TO_Course.Course_Id=TO_ClassExam.Course_ID  and TO_ClassExam.ClassID=" + ViewState["ClassID"].ToString();
        DataTable dt = obj.Query(search);



        search = "SELECT Course_Id, CourseName FROM TO_Course where Course_Id in (" + courseidlist + ") and Course_Id not in (select Course_ID from TO_ClassExam where ClassID=" + ViewState["ClassID"].ToString() + ")";
        DataTable dt01 = obj.Query(search);


        dt01.Columns.Add("PaperID");
        dt01.Columns.Add("PaperName");
        dt01.Columns.Add("ClassExam_ID");
        dt01.Columns.Add("ClassID");
        dt01.Columns.Add("Exam_ID");
        dt01.Columns.Add("Score");
        dt01.Columns.Add("ExamTime");


        for (int j = 0; j < dt01.Rows.Count; j++)
        {
            dt01.Rows[j]["PaperID"] = "0";
            dt01.Rows[j]["PaperName"] = "--";
            dt01.Rows[j]["ClassExam_ID"] = 0;
            dt01.Rows[j]["ClassID"] = 0;
            dt01.Rows[j]["Exam_ID"] = 0;
            dt01.Rows[j]["Score"] = 0;
            dt01.Rows[j]["ExamTime"] = 0;

            dt.Rows.Add(dt01.Rows[j]["Course_Id"], dt01.Rows[j]["CourseName"], dt01.Rows[j]["PaperID"], dt01.Rows[j]["PaperName"], dt01.Rows[j]["ClassExam_ID"], dt01.Rows[j]["ClassID"], dt01.Rows[j]["Exam_ID"], dt01.Rows[j]["Score"], dt01.Rows[j]["ExamTime"]);


        }

        search = "SELECT Paper.PaperID, Paper.PaperName, TO_ClassExam.ClassExam_ID,TO_ClassExam.ClassID, TO_ClassExam.Exam_ID, TO_ClassExam.Score,TO_ClassExam.ExamTime FROM Paper,TO_ClassExam where Paper.PaperID = TO_ClassExam.Exam_ID and TO_ClassExam.Course_ID=0  and TO_ClassExam.ClassID=" + ViewState["ClassID"].ToString();
        DataTable dt03 = obj.Query(search);
        dt03.Columns.Add("Course_Id");
        dt03.Columns.Add("CourseName");

        for (int k = 0; k < dt03.Rows.Count; k++)
        {
            dt03.Rows[k]["Course_Id"] = 0;
            dt03.Rows[k]["CourseName"] = "--";

            dt.Rows.Add(dt03.Rows[k]["Course_Id"], dt03.Rows[k]["CourseName"], dt03.Rows[k]["PaperID"], dt03.Rows[k]["PaperName"], dt03.Rows[k]["ClassExam_ID"], dt03.Rows[k]["ClassID"], dt03.Rows[k]["Exam_ID"], dt03.Rows[k]["Score"], dt03.Rows[k]["ExamTime"]);


        }

        dt.Columns.Add("UserScore");
        PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
        for (int m=0; m < dt.Rows.Count; m++)
        {
            search = "select UPHistoryScore from UserPaperHistory where FKUPHistoryUserID=" + login.UserID.ToString() + " and FKUPHistoryProjectID=" + voClass.ClassCourseListID.ToString() + " and FKUPHistoryClassID=" + dt.Rows[m]["ClassID"].ToString() + " and FKUPHistoryPaperID=" + dt.Rows[m]["Exam_ID"].ToString() + " and FKUPHistoryCourseID=" + dt.Rows[m]["Course_Id"].ToString();
            DataTable dt04 = ov.Query(search);
            if(dt04.Rows.Count>0)
                dt.Rows[m]["UserScore"] = dt04.Rows[0]["UPHistoryScore"];
            else
                dt.Rows[m]["UserScore"] ="--";

        }

        rptNew.DataSource = dt;
        rptNew.DataBind();
    }

    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lnkbtn_editor = (LinkButton)e.Item.FindControl("lnkbtn_editor");
            LinkButton lnkbtn_look = (LinkButton)e.Item.FindControl("lnkbtn_look");
            Label lbl_UserScore = (Label)e.Item.FindControl("lbl_UserScore");

            if (lbl_UserScore.Text == "--")
            {
                lnkbtn_editor.Enabled = true;
                lnkbtn_look.Enabled = false;
            }
            else
            {
                lnkbtn_editor.Enabled = false;
                lnkbtn_look.Enabled = true;
            }
        }
    }




    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        VOTOClassTasks vo = new VOTOClassTasks();
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");
        Label lbl_Exam_ID = (Label)e.Item.FindControl("lbl_Exam_ID");
        Label lbl_Course_ID = (Label)e.Item.FindControl("lbl_Course_ID");

        if (e.CommandName.ToLower() == "leditor")
        {
            string url = string.Format("../../web/UserObject/OpenPaperList.aspx?ProjectID={0}&ProjectClassID={1}&CourseID={2}&IsBuKao={3}&ProjectResourceID={4}&CourseResourceID={5}&ResourceID={6}", voClass.ClassCourseListID, ViewState["ClassID"], lbl_Course_ID.Text, "0", "0", "0", lbl_Exam_ID.Text);
            Script.NewOpenURL(url, "openpaper");

        }
        if (e.CommandName.ToLower() == "look")
        {
            string url = string.Format("../../web/UserObject/ReadedPaper.aspx?PaperID={0}&ProjectID={1}&ClassID={2}&CourseID={3}&UserID={4}", lbl_Exam_ID.Text, voClass.ClassCourseListID, ViewState["ClassID"], lbl_Course_ID.Text, login.UserID.ToString());
            Script.NewOpenURL(url, "openpaper");

        }
       

    }
   

}
